package com.app.micaihu.view.army.activity

import android.os.Bundle
import com.app.base.BaseMvpActivity
import com.app.http.bean.BaseBean
import com.app.micaihu.R
import com.app.micaihu.configure.Config
import com.app.micaihu.view.army.adapter.ArmyOfficialPositionSetListAdapter
import com.app.micaihu.view.army.contract.ArmyMemberContract
import com.app.micaihu.view.army.presenter.ArmyMemberPresenter
import com.app.micaihu.view.bean.ArmyMember
import com.app.micaihu.view.main.view.WrapContentLinearLayoutManager
import com.blankj.utilcode.util.CollectionUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.army_official_position_set_activity.*

/**
 * 军团官职设置列表
 */
class ArmyOfficialPositionSetListActivity : BaseMvpActivity<ArmyMemberContract.View, ArmyMemberPresenter>(), ArmyMemberContract.View {

    override fun getLayoutId() = R.layout.army_official_position_set_activity

    override fun getBundle(bundle: Bundle) {
        gid = bundle.getString(Config.Extra.id, "")
        level = bundle.getString(Config.Extra.level, "")
        maxSelect = bundle.getInt(Config.Extra.max, 0)
        selectMemberList = bundle.getSerializable(Config.Extra.list) as List<ArmyMember.Member>
    }

    override fun getPresenter() = ArmyMemberPresenter(gid)

    private var gid = ""
    private var level = ""
    private var maxSelect = 0

    private var selectMemberList: List<ArmyMember.Member>? = null
    private val armyOfficialPositionSetListAdapter: ArmyOfficialPositionSetListAdapter by lazy {
        val armyOfficialPositionSetListAdapter = ArmyOfficialPositionSetListAdapter(true)
        rvList.layoutManager = WrapContentLinearLayoutManager(thisContext)
        rvList.adapter = armyOfficialPositionSetListAdapter
        armyOfficialPositionSetListAdapter
    }

    override fun setData() {
        armyOfficialPositionSetListAdapter.setEmptyView(loadingView)
        mPresenter?.armyAppointList(gid)
    }

    override fun addListeners() {
        armyOfficialPositionSetListAdapter.setOnItemClickListener { adapter, view, position ->
            val item = armyOfficialPositionSetListAdapter.getItem(position)
            if (maxSelect == 0) {
                ToastUtils.showShort("选择已到上限")
                return@setOnItemClickListener
            }
            if (armyOfficialPositionSetListAdapter.getSelectSize() == maxSelect) {
                when {
                    item.position == "1" -> {
                        //创始人 不可选择
                    }
                    item.select -> {
                        item.select = !item.select
                        armyOfficialPositionSetListAdapter.notifyItemChanged(position + armyOfficialPositionSetListAdapter.headerLayoutCount)
                    }
                    else -> {
                        ToastUtils.showShort("选择已到上限")
                    }
                }
            } else {
                if (item.position != "1") {
                    item.select = !item.select
                    armyOfficialPositionSetListAdapter.notifyItemChanged(position + armyOfficialPositionSetListAdapter.headerLayoutCount)
                }
            }
            btnSet.text = "任命（${armyOfficialPositionSetListAdapter.getSelectSize()}/${maxSelect}）"
        }
        btnSet.setOnClickListener {
            mPresenter?.armyAppoint(gid, level, armyOfficialPositionSetListAdapter.getSelectUid())
        }
    }

    override fun handleArmyAppointList(result: BaseBean<ArmyMember?>?) {
        if (result == null) {
            armyOfficialPositionSetListAdapter.setEmptyView(errorView)
        } else {
            if (result.isSuccess()) {
                armyOfficialPositionSetListAdapter.setEmptyView(getEmptyView())
                result.data?.let { data ->
                    val memberList = data.memberList
                    if (selectMemberList != null && memberList != null) {
                        for (select in selectMemberList!!) {
                            val selectUid = select.uid
                            for (memberItem in memberList) {
                                if (!memberItem.select) {
                                    memberItem.select = memberItem.uid == selectUid
                                }
                            }
                        }
                    }
                    armyOfficialPositionSetListAdapter.setList(memberList)
                    btnSet.text = "任命（${armyOfficialPositionSetListAdapter.getSelectSize()}/${maxSelect}）"
                }
            } else {
                ToastUtils.showShort(result.msg)
            }
        }
    }

    override fun handleArmyAppoint(result: BaseBean<Any?>?) {
        if (result == null) {
            ToastUtils.showShort(getString(R.string.net_error))
        } else {
            if (result.isSuccess()) {
                LiveEventBus.get(Config.EventBus.armyAppointSuccess).post(true)
                finish()
            }
            ToastUtils.showShort(result.msg)
        }
    }

}